package com.djinfo.work.service.impl;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.service.impl.BaseServiceImpl;
import com.djinfo.work.dao.DictDao;
import com.djinfo.work.dao.UserDao;
import com.djinfo.work.dao.impl.DictDaoImpl;
import com.djinfo.work.entity.QsDictHospital;
import com.djinfo.work.entity.TbSysDict;
import com.djinfo.work.entity.TbSysUsers;
import com.djinfo.work.service.DictService;
import com.djinfo.work.service.QsUserQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by admin on 2017/7/11.
 */
@Service("DictService")
public class DictServiceImpl extends BaseServiceImpl<TbSysDict,DictDaoImpl> implements DictService {
    @Autowired
    private DictDao dictDao;
    @Autowired
    private QsUserQuestionService qsUserQuestionService;

    @Autowired
    private UserDao userDao;
    @Override
    public AjaxJson getProvinceCityHospitalList() {
        AjaxJson ajaxJson=new AjaxJson();
        List list2=new ArrayList();//最外层
        String hql1=" from TbSysDict where length(id)=8 ";//获取省份列表的hql语句
        List<TbSysDict> list=dictDao.getProvinceList(hql1);
        for(TbSysDict tbSysDict:list){
            List list1=new ArrayList();//用来包装城市
            HashMap map1=new HashMap();
            String hql2=" from TbSysDict where length(id)=12 and id like ?";//获取城市列表的hql语句
            List<TbSysDict> cityList=dictDao.getCityList(hql2,"%"+tbSysDict.getId()+"%");
            for(TbSysDict tbSysDict1:cityList){
                HashMap map2=new HashMap();
                String hql3=" from QsDictHospital where city like ?";
                List<QsDictHospital> saleHospList=dictDao.getHospitalList(hql3,"%"+tbSysDict1.getDval()+"%");
                map2.put("hospitals",saleHospList);
                map2.put("city",tbSysDict1);
                list1.add(map2);
            }
            map1.put("province",tbSysDict);
            map1.put("cities",list1);
            list2.add(map1);
        }
        HashMap map=new HashMap();
        map.put("data",list2);
        ajaxJson.setResult(map);
        return ajaxJson;
    }

    @Override
    public AjaxJson getProvinceList() {
        AjaxJson ajaxJson=new AjaxJson();

        HashMap map=new HashMap();

        String hql=" from TbSysDict where length(id)=8 ";//获取省份列表的hql语句
        List<TbSysDict> list=dictDao.getProvinceList(hql);
        map.put("provinceList",list);
        ajaxJson.setResult(map);

        return ajaxJson;
    }

    @Override
    public AjaxJson getCityList(String id) {
        AjaxJson ajaxJson=new AjaxJson();

        String hql=" from TbSysDict where length(id)=12 and id like ?";//获取城市列表的hql语句

        List<TbSysDict> cityList=dictDao.getCityList(hql,"%"+id+"%");

        HashMap map=new HashMap();

        map.put("cityList",cityList);

        ajaxJson.setResult(map);

        return ajaxJson;
    }

    @Override
    public AjaxJson getUserList(String userName) {

        AjaxJson ajaxJson=new AjaxJson();
        HashMap map=new HashMap();

        String hql="";
        if(userName==null||"".equals(userName)){
            hql = " from TbSysUsers where 1=1 ";
        }else {
            hql = " from TbSysUsers where userName like '%"+ userName+ "%'";
        }
        List<TbSysUsers> userList=userDao.getList(hql);

        map.put("userListt",userList);

        ajaxJson.setResult(map);

        return ajaxJson;
    }

    @Override
    public AjaxJson saveUserQuestion(QsDictHospital qsDictHospital) {

        AjaxJson ajaxJson=new AjaxJson();
        dao.save(qsDictHospital);
        return ajaxJson;
    }

    @Override
    public AjaxJson getHospitalList(HttpServletRequest request) {
        return null;
    }

    @Override
    public TbSysDict getTbSysDictByProvince(String province) {

        TbSysDict tbSysDict=new TbSysDict();
        tbSysDict.setId("");
        String hql=" from TbSysDict where dval='"+province+"'";

        List<TbSysDict> list=dictDao.getList(hql);
        if(list.size()!=0){
            tbSysDict=list.get(0);
        }

        return tbSysDict;
    }


}
